import 'dart:convert';

import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart' hide Action;
import 'package:flutter_inappbrowser/flutter_inappbrowser.dart';
import 'package:www.znpigai.teacher/global_store/state.dart';

import 'action.dart';
import 'state.dart';

Widget buildView(
    WebShareState state, Dispatch dispatch, ViewService viewService) {

  return Scaffold(
      appBar: AppBar(
        title: Text(state.title ?? ''),
        centerTitle: true,
        actions: <Widget>[
          IconButton(
            icon: new Icon(Icons.close),
            tooltip: 'Closes application',
            onPressed: () => Navigator.pop(viewService.context),
          ),
        ],
      ),
      body: Stack(
        children: <Widget>[
          Positioned(
            top: 0,
            left: 0,
            right: 0,
            bottom: 160,
            child: InAppWebView(
                  initialUrl: state.url,
                  initialHeaders: {'ticket': GlobalState.ticket},
                  initialOptions: {},
                  onWebViewCreated: (InAppWebViewController controller) {
                    state.controller = controller;
                    // post
                    if (state.postData != null) {
                      var postStr = '';
                      state.postData.forEach((k, v) {
                        postStr += '$k=$v&';
                      });
                      state.controller.postUrl(state.url, utf8.encode(postStr));
                    }
                  },
                  onLoadStart: (InAppWebViewController controller, String url) {
                    print("started $url");
                  },
                  onProgressChanged:
                      (InAppWebViewController controller, int progress) {
                    dispatch(WebShareActionCreator.progress(progress));
                  },
                  onLoadStop: (InAppWebViewController controller, String url) {
                    state.controller.getTitle().then((title) =>
                        dispatch(WebShareActionCreator.title(title)));
                  },
                ),
          ),
          Positioned(
              top: 0,
              left: 0,
              right: 0,
              child: Offstage(
                offstage: state.progress == 100,
                child: SizedBox(
                  height: 2.0,
                  child: LinearProgressIndicator(
                    valueColor: new AlwaysStoppedAnimation<Color>(Colors.green),
//                value: 80,
//                    backgroundColor: Colors.green,
                  ),
                ),
              )),
          Positioned(
              bottom: 0,
              left: 0,
              right: 0,
              height: 160,
              child: Container(
                  height: 160.0,
                  child: Stack(
                    children: <Widget>[
                      Container(
                        padding: const EdgeInsets.all(10.0),
                        child: Text("分享到", style: TextStyle(fontSize: 12)),
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: <Widget>[
                          CupertinoButton(
                              child: Padding(
                                  padding: EdgeInsets.all(10.0),
                                  child: Column(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    children: <Widget>[
                                      Image.asset("assets/images/save_btn.png",
                                          width: 50.0,
                                          height: 50.0,
                                          fit: BoxFit.fill),
                                      Text("保存分享海报",
                                          style: TextStyle(fontSize: 12))
                                    ],
                                  )),
                              onPressed: () {
                                dispatch(WebShareActionCreator.onSnap());
                              }),
                          CupertinoButton(
                              child: Padding(
                                  padding: EdgeInsets.all(10.0),
                                  child: Column(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    children: <Widget>[
                                      Image.asset("assets/images/share2.png",
                                          width: 50.0,
                                          height: 50.0,
                                          fit: BoxFit.fill),
                                      Text("去跟好友嘚瑟",
                                          style: TextStyle(fontSize: 12))
                                    ],
                                  )),
                              onPressed: () {
                                dispatch(WebShareActionCreator.onShare());
                              }),
                        ],
                      )
                    ],
                  ))),
        ],
      ));
}
